package com.project.controller;

import com.project.bean.EmployeeBean;
import com.project.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafCotroller {

//    @RequestMapping("/test")
//    public String testThy(HttpServletRequest request){
//        //在request请求对象中，绑定共享数据，数据只存在于一次请求范围内
//        request.setAttribute("warninfo", "成都疫情出现，戴好口罩");
//
//        return "redirect:https://www.baidu.com";
//    }

//    @RequestMapping("/test")
//    public String testThy(Model model){
//        //在请求范围内绑定共享数据，等同于request.setAttribute()
//        model.addAttribute("warninfo", "大家不要慌，淡定淡定");
//        return "/html/test.html";
//    }

    @RequestMapping("test")
    public ModelAndView testThy(){
        ModelAndView mv = new ModelAndView();
        //在请求范围内绑定共享数据
        mv.addObject("warninfo", "要相信政府，相信国家");
        mv.addObject("user",
                new UserBean(1, "李典", "18920392832", LocalDate.parse("1992-09-04")));

        mv.addObject("employee",
                new EmployeeBean(2, "何元庆", 4000, "在职"));

        mv.addObject("emList",
                Arrays.asList(new EmployeeBean(2, "何元庆", 4000, "在职"),
                        new EmployeeBean(8, "余涛", 8000, "离职"),
                        new EmployeeBean(6, "朱军", 6000, "在职")));

        Map<Integer,EmployeeBean> map = new HashMap<>();
        map.put(2, new EmployeeBean(2, "何元庆", 9000, "在职"));
        map.put(5, new EmployeeBean(5, "赵贤", 8000, "离职"));
        map.put(8, new EmployeeBean(8, "桂小宝", 6000, "在职"));
        mv.addObject("emMap", map);

        mv.addObject("num", 5);
        mv.addObject("imgPath", "win.jpg");
        //设置跳转的视图
        mv.setViewName("/html/test.html");
        return mv;
    }

    @RequestMapping("/emDel")
    @ResponseBody
    public String del(int id){

        return "删除id="+id+"员工";
    }
}
